//
//  CustomHeaderView.m
//  婚礼纪  首页
//
//  Created by 太阳 on 15/5/15.
//  Copyright (c) 2015年 三人. All rights reserved.
//

#import "CustomHeaderView.h"
#import "RecommendModel.h"
#import "ConnectionModel.h"
#import "UIImageView+WebCache.h"
@implementation CustomHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.myscroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width * 0.5)];
        self.myscroll.delegate = self;
        [self addSubview:self.myscroll];
        [self.myscroll setBackgroundColor:[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.6]];
        
        self.oneview = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.width * 0.5, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width *  0.24)];
        [self.oneview setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:self.oneview];
        
        self.twoview = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.width * 0.75, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width * 0.44)];
        [self.twoview setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.2]];
        [self addSubview:self.twoview];
        [self requestNet];
    }
    return self;
}

- (void)requestNet
{
    [ConnectionModel connectDoneWithStr:@"http://hunliji.com/v1/api/app/posters.json?app_version=5.2.0&cid=0&device=1" block:^(NSData *data) {
        self.bigarray = [NSMutableArray array];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dic objectForKey:@"sites"];
        
        for (NSDictionary *seconddic in array) {
            NSArray *secondarray = [seconddic objectForKey:@"posters"];
            NSMutableArray *moderarray = [NSMutableArray array];
            for (NSDictionary *third in secondarray) {
                RecommendModel *model = [[RecommendModel alloc]init];
                [model setValuesForKeysWithDictionary:third];
                [moderarray addObject:model];
            }
            [self.bigarray addObject:moderarray];
        }
        [self rollScroll]; //旋转图片
        [self createoneview];
        [self createtwoview];
    }];
}
- (void)createtwoview
{
    NSArray *array = [self.bigarray objectAtIndex:1];
    int i = 0;
    for (RecommendModel *model in array) {
        UIImageView *imageview =[[UIImageView alloc]initWithFrame:CGRectMake(18 + i * [UIScreen mainScreen].bounds.size.width / 3.2, 10, [UIScreen mainScreen].bounds.size.width / 3.0- 20, [UIScreen mainScreen].bounds.size.width / 3 / 0.79 - 20)];
        [imageview sd_setImageWithURL:[NSURL URLWithString:model.image_path] placeholderImage:nil options:SDWebImageRetryFailed];

        [self.twoview addSubview:imageview];
        i++;
    }
    
}
- (void)createoneview
{
    NSArray *array = [self.bigarray objectAtIndex:3];
    int i = 0;
    for (RecommendModel *model in array) {
        UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(18 + i * [UIScreen mainScreen].bounds.size.width / 4, 12, [UIScreen mainScreen].bounds.size.width / 4 - 35, 58)];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(13 + i * [UIScreen mainScreen].bounds.size.width / 4, 12 + 58, [UIScreen mainScreen].bounds.size.width / 4 - 25, 20)];
        label.text = model.title;
        label.font = [UIFont systemFontOfSize:13];
        label.textColor = [UIColor colorWithWhite:0.3 alpha:0.8];
        
        [self.oneview addSubview:label];
        label.textAlignment = NSTextAlignmentCenter;
//        [view setBackgroundColor:[UIColor greenColor]];
        [view sd_setImageWithURL:[NSURL URLWithString:model.image_path] placeholderImage:nil options:SDWebImageRetryFailed];
        [self.oneview addSubview:view];
        i++;
    }
}
- (void)rollScroll
{
    NSMutableArray *array = [self.bigarray objectAtIndex:0];
    id first = [array firstObject];
    id last = [array lastObject];
    
    [array addObject:first];
    [array insertObject:last atIndex:0];
    int i = 0;
    for (RecommendModel *model in array) {
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(i * [UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width * 0.5)];
        [imageview sd_setImageWithURL:[NSURL URLWithString:model.image_path] placeholderImage:nil options:SDWebImageRetryFailed];
        [self.myscroll addSubview:imageview];
        i ++;
        self.myscroll.contentSize = CGSizeMake(i * [UIScreen mainScreen].bounds.size.width, 0);
    }
    self.myscroll.pagingEnabled = YES;
    self.myscroll.clipsToBounds = NO;
    // 设置时间部分
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    //定义时间并调用timerAction方法
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}

- (void)timerAction:(NSTimer *)timer
{
    self.page++;
    CGFloat offWidth = self.myscroll.frame.size.width * self.page;
    NSInteger num = self.myscroll.contentSize.width / self.myscroll.frame.size.width;
    
    [UIView animateWithDuration:0.5 animations:^{
        [self.myscroll setContentOffset:CGPointMake(offWidth, 0)];
    }];
    if (self.page == num - 1) {
        self.page = 1;
        [self.myscroll setContentOffset:CGPointMake(self.myscroll.frame.size.width, 0)];
    }
}
@end
